package editadmin;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use base 'func';
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);
use strict;
use CGI::Carp qw(fatalsToBrowser);

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub return_edit_admin {

    my $self = shift;
    my $dd;

    my $sth = $self->{dbh}->prepare(
        'SELECT t1.id,
                                t1.name,
                                t1.info,
                                t1.active,
                                t1.created,
                                t2.m_mailbox,
                                t2.m_domain,
                                t2.m_alias,
                                t2.mb_domain,
                                t2.mb_mailbox
                        FROM users  AS t1
                        LEFT JOIN admin_domain AS t2
                        ON t1.id = t2.idname
                        WHERE  t1.id = ?
                        LIMIT 1
                          '
    );
    $sth->execute( $self->{p}->{username} );

    $dd = $sth->fetchrow_hashref();
    $sth->finish();

    my $sth = $self->{dbh}->prepare(
        'SELECT domain 
                         FROM domain
                         WHERE idnane = ? 
                         ORDER by domain asc'
    );
    $sth->execute( $self->{p}->{username} );
    my $rows2;
    push @{$rows2}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    my $sth = $self->{dbh}->prepare(
        'SELECT domain 
                         FROM domain 
                         ORDER by domain asc'
    );
    $sth->execute();
    my $rows;
    push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    if ( !$rows ) {
        $self->{he}->{NO_DOMAIN} = 1;
    }
    else {
        $self->{he}->{ROWS} = $rows;
    }

    if ( !$rows2 ) {
        $self->{he}->{NO_DOMAIN2} = 1;
    }
    else {
        $self->{he}->{ROWS2} = $rows2;
    }

    if ( $dd->{m_alias} > 0 || $dd->{m_alias} == 0 ) {
        $dd->{createalias_too} = 1;
    }
    else {
        $dd->{createalias_too} = undef;
    }

    $dd->{mb_domain} = sprintf( '%.f', $dd->{mb_domain} / 1048576 )
      if ( $dd->{mb_domain} > 0 );

    $dd->{mb_mailbox} = sprintf( '%.f', $dd->{mb_mailbox} / 1048576 )
      if ( $dd->{mb_mailbox} > 0 );

    $dd->{createdomain_too} = 1 if ( $dd->{m_domain} ne '' );
    $dd->{m_alias} = '' if ( $dd->{m_alias} == -1 );

    while ( my ( $key, $value ) = each( %{$dd} ) ) {
        $self->{he}->{$key} = $value;
    }

    return $self;
}

sub templ_hash_admin {
    my $self = shift;
    $self->{session}->{filet} = 'edit-admin';
    $self->SUPER::templ_hash(@_);
    return $self;
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
